package com.wedo.star.controller;

import com.wedo.auth.pojo.UserInfo;
import com.wedo.auth.utils.JwtUtils;
import com.wedo.common.pojo.PageResult;
import com.wedo.star.config.JwtProperties;
import com.wedo.star.pojo.BookStar;
import com.wedo.star.result.ResultStar;
import com.wedo.star.service.StarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("userStar")
public class StarController {

    @Autowired
    private StarService starService;

    @Autowired
    private JwtProperties jwtProperties;

    @GetMapping("page")
    public ResponseEntity<PageResult<ResultStar>> queryStarPageByUid(
            @RequestHeader("WD_TOKEN") String token,
            @RequestParam(value = "bid",required = false) Long bid,
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "6") Integer rows
    ) {
        UserInfo userInfo = null;
        // 1.解析token
        try {
            userInfo = JwtUtils.getInfoFromToken(token,jwtProperties.getPublicKey());
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
        }
        PageResult<ResultStar> result = this.starService.queryStarPageByUid(userInfo.getId(),bid,page,rows);
        if (result == null || CollectionUtils.isEmpty(result.getItems())) {
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        }
        return ResponseEntity.ok(result);
    }

    @PostMapping
    public ResponseEntity<BookStar> addBookStar(
            @RequestHeader("WD_TOKEN") String token,
            @RequestBody BookStar bookStar
    ) {
        UserInfo userInfo = null;
        // 1.解析token
        try {
            userInfo = JwtUtils.getInfoFromToken(token,jwtProperties.getPublicKey());
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
        }
        bookStar.setuId(userInfo.getId());
        bookStar = this.starService.addBookStar(bookStar);
        if (bookStar == null) {
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        }
        return ResponseEntity.ok(bookStar);
    }

    @PutMapping
    public ResponseEntity<BookStar> updateBookStar(
            @RequestHeader("WD_TOKEN") String token,
            @RequestBody BookStar bookStar
    ) {
        UserInfo userInfo = null;
        // 1.解析token
        try {
            userInfo = JwtUtils.getInfoFromToken(token,jwtProperties.getPublicKey());
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
        }
        bookStar.setuId(userInfo.getId());
        bookStar = this.starService.updateBookStar(bookStar);
        if (bookStar == null) {
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        }
        return ResponseEntity.ok(bookStar);
    }

    @DeleteMapping
    public ResponseEntity<BookStar> deleteBookStar(
            @RequestHeader("WD_TOKEN") String token,
            @RequestBody BookStar bookStar
    ) {
        UserInfo userInfo = null;
        // 1.解析token
        try {
            userInfo = JwtUtils.getInfoFromToken(token,jwtProperties.getPublicKey());
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
        }
        bookStar.setuId(userInfo.getId());
        bookStar = this.starService.deleteBookStar(bookStar);
        if (bookStar == null) {
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        }
        return ResponseEntity.ok(bookStar);
    }

}
